<?php
/*
*
* -------------------------------------------------------
* CLASSNAME:        brand
* GENERATION DATE:  29.04.2013
* CLASS FILE:       D:\wamp\www\webpros\webpros_sites\webpros\module-generator/generated-classes/class.brand.inc.php
* FOR MYSQL TABLE:  brand
* FOR MYSQL DB:     modulegenerator
* -------------------------------------------------------
* -------------------------------------------------------
*
*/

class brand {
/**
*   @desc Variable Declaration with default value
*/
	private $_id;   // KEY ATTR. WITH AUTOINCREMENT
     private $_name;  	 	 private $_alias; ###
     private $_image;  	 	 private $_home_image;  	 
     private $_description;  
     private $_addeddate;  
     private $_updateddate;  
     private $_status;  
     
/**
*   @desc   CONSTRUCTOR METHOD
*/
	function __construct() {
		global $DBOBJ;
		$this->_obj = $DBOBJ;
		$this->_id = null; 
		$this->_name = null; 		$this->_alias = null; ###				$this->_home_image = null; 
		$this->_image = null; 
		$this->_description = null; 
		$this->_addeddate = null; 
		$this->_updateddate = null; 
		$this->_status = null; 
	}

/**
*   @desc   DECONSTRUCTOR METHOD
*/
	function __destruct() {
		unset($this->_obj);
	}

/**
*   @desc   GETTER METHODS
*/

     public function getid() {
     	return $this->_id;
     }
     
     public function getname() {
     	return $this->_name;
     }	 	 #####	public function getalias() {        return $this->_alias;    }
     		public function gethome_image() {     	return $this->_home_image;    }	      public function getimage() {
     	return $this->_image;
     }
     
     public function getdescription() {
     	return $this->_description;
     }
     
     public function getaddeddate() {
     	return $this->_addeddate;
     }
     
     public function getupdateddate() {
     	return $this->_updateddate;
     }
     
     public function getstatus() {
     	return $this->_status;
     }
     
/**
*   @desc   SETTER METHODS
*/

     public function setid($id) {
     	 $this->_id =  $id;
     }
     
     public function setname($name) {
     	 $this->_name =  $name;
     }
     	 ###	public function setalias($alias) {        $this->_alias = $alias;    }	 	 	 	 public function sethome_image($home_image) {     	 $this->_home_image =  $home_image;     }	 	 
     public function setimage($image) {
     	 $this->_image =  $image;
     }
     
     public function setdescription($description) {
     	 $this->_description =  $description;
     }
     
     public function setaddeddate($addeddate) {
     	 $this->_addeddate =  $addeddate;
     }
     
     public function setupdateddate($updateddate) {
     	 $this->_updateddate =  $updateddate;
     }
     
     public function setstatus($status) {
     	 $this->_status =  $status;
     }
     
/**
*   @desc   SELECT METHOD / LOAD
*/

	public function select($id) {
		 $sql =  "SELECT * FROM `brand` WHERE `id` = '". $id ."' ";
		 $row =  $this->_obj->select($sql); 

		 if (count($row) > 0) {
			 $this->_id = $row[0]['id'];
     		 $this->_name = $row[0]['name'];			 $this->_alias = $row[0]['alias'];  ###							$this->_home_image = $row[0]['home_image'];
     		 $this->_image = $row[0]['image'];
     		 $this->_description = $row[0]['description'];
     		 $this->_addeddate = $row[0]['addeddate'];
     		 $this->_updateddate = $row[0]['updateddate'];
     		 $this->_status = $row[0]['status'];
     			 return $row[0];
		}else {
			 return;
		}
	}
/**
*   @desc   set All
*/
	 public function setAll($dataArray = array()) {
		 if (count($dataArray) <= 0) {
			return;
		}

		foreach ($dataArray as $key => $value) {
			$classKey = '_' . $key;
			$this->$classKey = $value;
		}
	}

/**
*   @desc   get All
*/

	 public function getAll($FORMAT = 'ARRAY') {
		$dataArray = new stdClass();
		$dataArray->id = $this->getid();
     	$dataArray->name = $this->getname();		$dataArray->alias = $this->getalias();  ###				$dataArray->home_image = $this->gethome_image();		
     	$dataArray->image = $this->getimage();
     	$dataArray->description = $this->getdescription();
     	$dataArray->addeddate = $this->getaddeddate();
     	$dataArray->updateddate = $this->getupdateddate();
     	$dataArray->status = $this->getstatus();
     	if ($FORMAT == 'ARRAY') {
			return (array) $dataArray;
		}
		return $dataArray;
	}
/**
*   @desc   COUNT
*/

	public function getCountbrand() {
		 $sql = "SELECT count(*) as cnt  FROM `brand`";
		 $result = $this->_obj->select($sql);
		 return $result['0']['cnt'];
	}

/**
*   @desc   DELETE
*/
	public function delete($id) {
		 $sql = "DELETE FROM `brand` WHERE `id` = '". $id ."'";
		 $result = $this->_obj->sql_query($sql);
	}

/**
*   @desc   INSERT
*/
	public function insert() {
		 $this->id = ""; // clear key for autoincrement
		 $sql = "INSERT INTO `brand` ( `id`,`name`,`alias`,`image`,`home_image`,`description`,`addeddate`,`updateddate`,`status` ) VALUES ( NULL,'".$this->_name."','".$this->_alias."','".$this->_image."','".$this->_home_image."','".$this->_description."','".$this->_addeddate."','".$this->_updateddate."','".$this->_status."' )";
		 $result = $this->_obj->insert($sql);
		 return $result;
	}

/**
*   @desc   UPDATE
*/	public function update($id) {
		 $sql = " UPDATE `brand` SET  `id` = '".$this->_id."' , `name` = '".$this->_name."' ,`alias` = '".$this->_alias."' , `image` = '".$this->_image."', `home_image` = '".$this->_home_image."'  , `description` = '".$this->_description."' , `addeddate` = '".$this->_addeddate."' , `updateddate` = '".$this->_updateddate."' , `status` = '".$this->_status."'  WHERE `id` =   '".$id."' ";
		 $result = $this->_obj->sql_query($sql);
	}
	public function getAllBrandList($params = "*", $where = "", $orderby = "id", $limit = "") {
		$orderby = "ORDER BY " . $orderby;
		$sql = "SELECT " . $params . " FROM `brand` a  " . $where . " " . $orderby . " " . $limit;
		$row = $this->_obj->select($sql);
		if ($row) {
			return $row;
		}
		return array();
	}
	#####	public function checkBrandAliasExists($alias) {        $sql = "SELECT * FROM `brand` WHERE `alias` = '".$alias."' limit 1";        $row = $this->_obj->select($sql);        if (count($row) > 0) {            return true;        } else {            return false;        }    }		####	//In the Edit mode 	public function checkBrandAlias($currentAlias = '' ,$alias) {        $sql = "select * from brand where alias = '" . $currentAlias . "' or alias = '".$alias."' ";        $row = $this->_obj->select($sql);        if (count($row) > 1) {            return false;        }        return true;    }		public function performListAction($action = "", $ids = "''") {
		switch ($action) {
			case "delete":
				$idArray = explode(',', $ids);
				if (count($idArray) > 0) {
					foreach ($idArray as $_id) {
						$this->select($_id);
						$this->deleteImage($this->getimage());						$this->deleteHomeImage($this->gethome_image());
					}
				}
				$sql = "DELETE FROM `brand` WHERE `id` IN (" . $ids . ")";
			break;
			default:
				$sql = "";
		}
		if ($sql == "") {
			return false;
		}
		$result = $this->_obj->sql_query($sql);
		return true;
    }
	
	public function checkBrandExists($name) {
		$sql = "select * from `brand` where name = '" . $name . "' limit 1";
		$row = $this->_obj->select($sql);
		if ($row) {
			return true;
		}
		return false;
	}
	
	
	

	public function updateImage($id = "", $filename = "") {
		if ($id == "") {
			return false;
		}
		$updateImageQuery = "UPDATE brand SET  image = '" . $filename . "' WHERE `id` = '" . $id . "'";
		$this->_obj->sql_query($updateImageQuery);
    }
	public function updateHomeImage($id = "", $filename = "") {		if ($id == "") {			return false;		}		$updateImageQuery = "UPDATE brand SET  home_image = '" . $filename . "' WHERE `id` = '" . $id . "'";		$this->_obj->sql_query($updateImageQuery);    }
		public function deleteHomeImage($name) {		$sourceFolder = "../upload/images/brand/home/";		$imagepath = $sourceFolder . trim($name);		$thumb_imagepath = $sourceFolder . trim("thumb_".$name);						if (file_exists($imagepath)) {			@unlink($imagepath);		}		if (file_exists($thumb_imagepath)) {			@unlink($thumb_imagepath);		}			}	

	public function deleteImage($name) {
		$sourceFolder = "../upload/images/brand/";
		$imagepath = $sourceFolder . trim($name);
		$thumb_imagepath = $sourceFolder . trim("thumb_".$name);				$medium_imagepath = $sourceFolder . trim("medium_".$name);		if (file_exists($imagepath)) {			@unlink($imagepath);		}
		if (file_exists($thumb_imagepath)) {			@unlink($thumb_imagepath);		}				if (file_exists($medium_imagepath)) {			@unlink($medium_imagepath);		}
	}
	
	public function checkBrandExistInProduct($brandId = '') {
		$sql = "SELECT COUNT(bid) AS 'cnt' FROM product WHERE bid= '".$brandId."'";
		$result = $this->_obj->select($sql);
		return $result['0']['cnt'];
	}
	
	public function brandDelete($dataArray = Array()) {
		$brandid = $dataArray['brandid'];
		$count = $this->checkBrandExistInProduct($brandid);  
		if ( $count >0 ) {
			echo 'UNSUCCESS';
		} else {
			$this->performListAction('delete', $brandid);						$deleteSql = "DELETE FROM `brand_to_category` WHERE `bid` = '".$brandid."'";			$result = $this->_obj->sql_query($deleteSql);			
		}
		exit;
	}

} 
?>